package com.lee.demo.ui.activity;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.lee.demo.R;
import com.lee.demo.util.per.AppPermissions;
import cn.lee.cplibrary.util.permissionutil.CPPermissionUtil;

import cn.lee.cplibrary.util.ToastUtil;

/**
 * 权限使用的Demo
 * create by ChrisLee on 2021/10/27
 */
public class PermissionActivity extends AppCompatActivity {
    private CPPermissionUtil permissionUtil;//用于模式1
    private CPPermissionUtil permissionUti2;//用于模式2
    private boolean checkPerms = true;//是否检测权限,用于模式1
    private PermissionActivity activity;
    private TextView tv_message;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(cn.lee.cplibrary.R.layout.cp_layout_empty);
        activity = this;
        permissionUtil = new CPPermissionUtil(activity);

        tv_message = findViewById(R.id.tv_message);

        //模式2：请求权限
        permissionUti2 = new CPPermissionUtil(activity);
        findViewById(R.id.iv_img).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                permissionUti2.setCallBack(new CPPermissionUtil.GrantedCallBack() {
                    @Override
                    public void granted(Object source, int requestCode) {
                        ToastUtil.showToast(activity, "允许了照相权限");
                    }

                    @Override
                    public void denied() {

                    }

                    @Override
                    public void needCheck() {

                    }
                }).requestPermissions( AppPermissions.Photo.PES_CAMERA, AppPermissions.Photo.PES_REQUESTCODE_PHOTO);
            }
        });

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode==AppPermissions.GaoDe.PES_REQUESTCODE){
            permissionUtil.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }else if (requestCode==AppPermissions.Photo.PES_REQUESTCODE_PHOTO){
            permissionUti2.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    //模式1：请求权限
    @Override
    protected void onResume() {
        super.onResume();
        if (checkPerms) {
            requestPers();
        }
    }

    private void requestPers() {
        checkPerms = false;
        String[] pers = (Build.VERSION.SDK_INT > 28
                && getApplicationInfo().targetSdkVersion > 28) ?
                AppPermissions.GaoDe.PES_GD28 : AppPermissions.GaoDe.PES_GD;
        //先setCallBack 后 requestPermissions
        permissionUtil.setCallBack(new CPPermissionUtil.GrantedCallBack() {
            @Override
            public void granted(Object source, int requestCode) {
                ToastUtil.showToast(activity, "允许了所有权限");
                tv_message.setText("允许了所有权限,点击图片有惊喜");
            }

            @Override
            public void denied() {
                finish();
            }

            @Override
            public void needCheck() {
                checkPerms = true;
            }
        }).requestPermissions(pers, AppPermissions.GaoDe.PES_REQUESTCODE);
    }
}
